package com.dimenlink.admin.controller;

import com.dimenlink.component.RedisComponent;
import com.dimenlink.entity.config.AppConfig;
import com.dimenlink.entity.constants.Constants;
import com.dimenlink.entity.dto.TokenUserInfoDto;
import com.dimenlink.entity.vo.ResponseVO;
import com.dimenlink.exception.BusinessException;
import com.dimenlink.service.UserInfoService;
import com.dimenlink.utils.StringTools;
import com.wf.captcha.ArithmeticCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息 Controller
 */
@Slf4j
@RestController
@RequestMapping("/account")
@Validated
public class AccountController extends ABaseController{

	@Resource
	private RedisComponent redisComponent;
	@Resource
	private AppConfig appConfig;

	@RequestMapping("/checkCode")
	public ResponseVO checkCode(){
		ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
		String code = captcha.text();
		String checkCodeKey = redisComponent.saveCheckCode(code);
		String checkCodeBase64 = captcha.toBase64();
		Map<String, String> result = new HashMap<>();
		result.put("checkCodeBase64", checkCodeBase64);
		result.put("checkCodeKey", checkCodeKey);
		log.info(result.toString());
		return getSuccessResponseVO(result);
	}

	@RequestMapping("/login")
	public ResponseVO login(HttpServletRequest request,
			HttpServletResponse response,
							@NotEmpty String account,
							@NotEmpty String password,
							@NotEmpty String checkCodeKey,
							@NotEmpty String checkCode){
		try{
			if(!checkCode.equals(redisComponent.getCheckCode(checkCodeKey))){
				throw new BusinessException("图片验证码错误");
			}

			//TODO md5
			if(!account.equals(appConfig.getAdminAccount())||!password.equals(appConfig.getAdminPassword())){
				throw new BusinessException("用户名或密码错误");
			}
			String token = redisComponent.saveTokenInfo4Admin(account);
			saveToken2Cookie(response, token);
			return getSuccessResponseVO(account);
		}finally {
			redisComponent.cleanCheckCode(checkCodeKey);

			Cookie[] cookies = request.getCookies();
			String token = null;
			if(cookies != null){
				for(Cookie cookie : cookies){
					if(cookie.getName().equals(Constants.TOKEN_ADMIN)){
						token = cookie.getValue();
					}
				}
				if(!StringTools.isEmpty(token)){
					redisComponent.cleanToken4Admin(token);
				}
			}
		}
	}


	@RequestMapping("/logout")
	public ResponseVO logout(HttpServletResponse response){
		cleanCookie( response);
		return getSuccessResponseVO(null);
	}






}